package com.itzxx.huazhun.ui

import com.itzxx.huazhun.data.model.User
import com.itzxx.huazhun.data.model.UserInfo
import com.itzxx.huazhun.data.repository.HttpRequestCoroutine
import com.itzxx.huazhun.utils.UserCacheHelper
import com.itzxx.huazhun.utils.ble.BleHelper
import com.itzxx.library_base_ktx.base.viewmodel.BaseViewModel
import com.itzxx.library_base_ktx.callback.livedata.BooleanLiveData
import com.itzxx.library_base_ktx.callback.livedata.event.EventLiveData
import com.itzxx.library_base_ktx.ext.request

/**
 * Desc:
 *
 * Author: zhangxiaoxiao
 * Date: 2024/6/25 13:49
 * Updater:
 * Update Time:
 * Update Comments:
 */
class MainViewModel : BaseViewModel() {

    val userPermissionLiveData = EventLiveData<User>()
    val userInfoLiveData = EventLiveData<UserInfo>()
    val isOfflineLiveData = BooleanLiveData()



    fun getUserInfo() {
        request({
            HttpRequestCoroutine.getUserInfo()
        }, {
            UserCacheHelper.putUserInfo(it)
            userInfoLiveData.postValue(it)
        }, {
            toast(it.errorMsg)
        })
    }


    override fun onCreateViewModel() {
        super.onCreateViewModel()
        getUserInfo()
        UserCacheHelper.getUserPermission()?.let {
            userPermissionLiveData.postValue(it.user)
        }

    }

    override fun onDestroyViewModel() {
        super.onDestroyViewModel()
        BleHelper.released()
    }
}